<template>
  <div class="container">
    <template v-for="(item, index) in form">
    <el-form ref="form" :model="item" label-width="110px">
        <el-form-item label="标题文字" v-if="item.isTitle" :key="index">
          <el-input v-model="item.title"></el-input>
        </el-form-item>
        <div class="model-item" v-else>
          <div class="head">
            <span>模块{{modelIndex[index-1]}}</span>
          </div>
          <div style="padding-right:16px">
            <el-form-item label="模块背景">
              <el-upload
              action=""
              list-type="picture-card"
              :http-request="(file)=>handleCustomUpload(file,index,1)"
              :show-file-list="false"
            >
              <div class="modelBox" v-if="item.url.url" @click.stop>
                <div class="model">
                  <i class="el-icon-zoom-in" @click="handlePictureCardPreview(1,item)"></i>
                  <i class="el-icon-delete" @click="handleDeleteUpload(1,index)"></i>
                </div>
                <img  :src="item.url.url" class="el-upload--picture-card" />
              </div>
              <i slot="default" class="el-icon-plus" v-else></i>
            </el-upload>
            <div class="tips">请上传jpg、png格式，尺寸建议为350px*420px</div>
            </el-form-item>
            <el-form-item label="模块标题">
              <el-input v-model="item.title"></el-input>
            </el-form-item>
            <div class="list">
              <div class="list-item" v-for="(k,i) in item.urlList" :key="i">
                <div class="head">分类{{ modelIndex[i] }}</div>
                <el-form-item label="图标">
                  <el-upload
                    action=""
                    list-type="picture-card"
                    :http-request="(file)=>handleCustomUpload(file,index,2,i)"
                    :show-file-list="false"
                  >
                    <div class="modelBox" v-if="k.url" @click.stop>
                      <div class="model">
                        <i class="el-icon-zoom-in" @click="handlePictureCardPreview(2,k.url)"></i>
                        <i class="el-icon-delete" @click="handleDeleteUpload(2,index,i)"></i>
                      </div>
                      <img  :src="k.url" class="el-upload--picture-card" />
                    </div>
                    <i slot="default" class="el-icon-plus" v-else></i>
                  </el-upload>
                  <span>请上传png格式，尺寸建议为24px*24px</span>
                </el-form-item>
                <el-form-item label="文字" style="margin:6px 6px 6px 0">
                  <el-input v-model="k.name"></el-input>
                </el-form-item>
              </div>
            </div>
          </div>
        </div>
      </el-form>
    </template>
    <div class="save">
      <el-button type="primary" @click="handleSave" style="margin-top:16px;">保存</el-button>
    </div>
    <el-image-viewer
      v-if="isShowImgView"
      :url-list="dialogImageUrl"
      :initial-index="0"
      :on-close="closeViewer"
      :z-index="3000"
    />
  </div>
</template>
<script>
  import {
    arraySort
  } from "@/utils";
  import naTable from "@/components/naTable";
  import {
    mapGetters
  } from "vuex";
  export default {
    name: "Materiel",
    components: {
      "el-image-viewer": () =>
      import("element-ui/packages/image/src/image-viewer"),
    },
    data() {
      return {
        form:{},
        dialogImageUrl:[],
        isShowImgView:false,
        modelIndex:['一','二','三','四','五']
        };
    },
    computed: {
      ...mapGetters(["name"]),
    },
    created() {
    },
    mounted() {
      this.getData()
    },
    updated() {},
    methods: {
      getData(){
        this.$api.system.GetList(4).then((res)=>{
        this.form = res
        this.form.forEach((item)=>{
          if(!item.isTitle && item.urlList.length<5){
            let count = 5-item.urlList.length
            let copyObj = JSON.parse(JSON.stringify(item.urlList[item.urlList.length-1]))
            delete copyObj.id
            copyObj.url=''
            copyObj.name=''
            for (let index = 0; index < count; index++) {
              item.urlList.push(JSON.parse(JSON.stringify(copyObj)))
            }
          }
        })
        console.log(this.form)
        })
      },

    handleSave(){
      this.form.forEach((item)=>{
          if(!item.isTitle){
            item.urlList = item.urlList.filter((x)=>{
              return x.name || x.url
            })
          }
        })
      this.$api.system.UpdateMore(this.form).then(res=>{
          this.$message.success('保存成功！')
          this.getData()
        })
    },

    handleCustomUpload(e,index,state,i){
      const file = e.file;
      const type = file.name.substring(file.name.lastIndexOf(".") + 1);
      if (
        type !== "png" &&
        type !== "jpg" &&
        type !== "jpeg" &&
        type !== "gif" &&
        type !== "bmp"
      ) {
        this.$message.error("上传图片格式错误，请重新上传!");
        return false;
      } else {
          var formData = new FormData();
            formData.append("file", file);
            formData.append("output", 'json2');
            this.$api.system.upload(formData).then((res) => {
              let prefix = '/assets'
              if(state == 1){
                this.form[index].url.url = prefix+res.src
              }else{
                this.form[index].urlList[i].url = prefix+res.src
              }
              this.$message.success("上传成功！");
            });
      }
    },
    handlePictureCardPreview(type,row){
      this.dialogImageUrl = []
      if(type == 1){
        this.dialogImageUrl.push(row.url.url)
      }else{
        this.dialogImageUrl.push(row)
      }
      this.isShowImgView = true
    },

    //删除图片
    handleDeleteUpload(type,index,i){
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          if(type == 1){
           this.form[index].url.url = ''
          }else{
            this.form[index].urlList[i].url = ''
          }
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
        });
      },

    closeViewer(){
      this.isShowImgView = false
    }
    },
  };

</script>
<style lang="scss" scoped>
.container::v-deep{
  padding:16px;
  overflow-y:auto;
  height:100%;
  $size:148px;
  .tips{
    font-size:13px;
    color:rgba(153, 153, 153, 1);
  }

  .list{
    margin-left:45px;
  }

  .list-item{
    width: 305px;
    border: 1px solid #e8e8e8;
    .head{
      margin-bottom: 6px;
      padding-left: 16px;
      display: flex;
      align-items: center;
      height: 40px;
      opacity: 1;
      background: rgba(253, 240, 255, 1);
    }

    .el-form-item__label{
      width: 60px !important;
    }

    .el-form-item__content{
      display: flex;
      align-items: center;
      margin-left: 60px !important;
      span{
        padding-left: 13px;
        line-height: 20px;
        font-size: 14px;
        color: rgba(153, 153, 153, 1);
      }
    }
    img{
      max-width: 100%;
      height: 100%;
      line-height: 80px;
    }

    .modelBox{
      height: 100%;
      .el-icon-zoom-in{
        margin-right:10px !important;
      }
    }

    .el-icon-plus{
      line-height: 80px;
    }

    .el-upload{
      width: 70px;
      height: 70px;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }

  .save{
    display:flex;
    justify-content:center;
  }
  .modelBox{
    .model{
      display: none;
      position: absolute;
      left: 0;
      top: 0;
      right: 0;
      bottom: 0;
      border-radius:6px;
      background-color: rgba(0, 0, 0, 0.5);
      z-index: 2;
      >*{
        font-size: 20px;
        color: #fff;
        &:hover{
          color: #3f61f1;
        }
        &:not(:last-child){
          margin-right: 20px;
        }
      }
    }
    &:hover{
      .model{
        display: flex;
        align-items: center;
        justify-content: center;
      }
    }
  }
  .el-upload{
    width: #{$size};
    height: #{$size};
    position: relative;
    .tag{
      position: absolute;
      left: 0;
      top: 0;
      padding: 4px 6px;
      line-height: 1.2em;
      background: rgba(0, 0, 0, 0.75);
      color: #fff;
      font-size: 12px;
      border-radius: 6px 0 4px 0;
      z-index: 2;
    }
  }
  .img_box{
    width: #{$size};
    height: #{$size};
    position: relative;
  }
  .img_boxMore{
    display: flex;
    flex-wrap: wrap;
    margin-right: -8px;
    margin-bottom: -8px;
    .img_box{
      border-radius:8px;
      border: 1px #dcdfe6 solid;
      overflow: hidden;
      margin-right: 8px;
    }
    >*{
      margin-right: 8px;
      margin-bottom: 8px;
    }
  }
}
  .model-item{
    margin-top:10px;
    border:1px solid #e8e8e8;
    padding-bottom:10px;
    .head{
      margin-bottom:16px;
      padding-left:30px;
      display:flex;
      align-items:center;
      height:40px;
      background: rgba(253, 240, 255, 1);
    }
  }
</style>
